package com.blrs.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.VO.EnterpriseHonorVO;
import com.blrs.common.annotation.Anonymous;
import com.blrs.common.constant.HttpStatus;
import com.blrs.common.core.domain.entity.SysDictData;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.EnterpriseInfo;
import com.blrs.domain.HonerManagementAttachment;
import com.blrs.domain.HonorManagement;
import com.blrs.domain.vo.HonorPublicVO;
import com.blrs.service.honor.IHonerManagementAttachmentService;
import com.blrs.service.honor.IHonorManagementService;
import com.blrs.system.domain.SysUploadFile;
import com.blrs.system.service.ISysDeptService;
import com.blrs.system.service.ISysDictTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 荣誉资质信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/Honor/management")
public class HonorManagementController extends BaseController
 {
    @Autowired
    private IHonorManagementService honorManagementService;
    @Autowired
    private IHonerManagementAttachmentService honerManagementAttachmentService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询荣誉资质信息列表
     */
    //@PreAuthorize("@ss.hasPermi('Honor:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(HonorManagement honorManagement,@RequestParam Integer pageNum, @RequestParam Integer pageSize)
    {
        //startPage();
        List<HonorManagement> list = honorManagementService.selectHonorManagementList(honorManagement);
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, list.size());
        List<HonorManagement> pageList = list.subList(start, end);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageList);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
        //return getDataTable(list);
    }

     /**
      * 查询荣誉资质首页信息
      * @param honorManagement
      * @return
      */
     @GetMapping("/listIndex")
     public TableDataInfo listIndex(HonorManagement honorManagement,@RequestParam Integer pageNum, @RequestParam Integer pageSize)
     {
         List<HonorManagement> list = honorManagementService.selectHonorManagementListIndex(honorManagement);
         int start = (pageNum - 1) * pageSize;
         int end = Math.min(start + pageSize, list.size());
         List<HonorManagement> pageList = list.subList(start, end);
         TableDataInfo rspData = new TableDataInfo();
         rspData.setCode(HttpStatus.SUCCESS);
         rspData.setMsg("查询成功");
         rspData.setRows(pageList);
         rspData.setTotal(new PageInfo(list).getTotal());
         return rspData;
     }


     @GetMapping("/listOrgId")
     public TableDataInfo listOrgId(HonorManagement honorManagement)
     {
         startPage();
         List<HonorManagement> list = honorManagementService.selectHonorManagementListByOrgId(honorManagement);
         return getDataTable(list);
     }

     @GetMapping("/listType")
     public Object listType(HonorManagement honorManagement)
     {
         List<HonorManagement> list = honorManagementService.selectHonorManagementListAll(honorManagement);
         return list;
     }

     @Log(title = "导入荣誉资质", businessType = BusinessType.IMPORT)
     @PostMapping("/importData")
     @Anonymous
     public String importData(MultipartFile file) throws Exception
     {
         ExcelUtil<HonorManagement> util = new ExcelUtil(HonorManagement.class);
         List<HonorManagement> list = util.importExcel(file.getInputStream());
         return honorManagementService.importExcel(list);
     }
     @Log(title = "导入荣誉资质（公海）", businessType = BusinessType.IMPORT)
     @PostMapping("/importDataSeas")
     @Anonymous
     public String importDataSeas(MultipartFile file) throws Exception
     {
         ExcelUtil<HonorManagement> util = new ExcelUtil(HonorManagement.class);
         List<HonorManagement> list = util.importExcel(file.getInputStream());
         return honorManagementService.importExcelSeas(list);
     }

    /**
     * 导出荣誉资质信息列表
     */
    //@PreAuthorize("@ss.hasPermi('Honor:management:export')")
//    @Log(title = "荣誉资质信息", businessType = BusinessType.EXPORT)
// @PostMapping("/export")
// @Anonymous
// public void export(HttpServletResponse response, HonorManagement honorManagement)
// {
//     List<HonorManagement> list = honorManagementService.selectHonorManagementList(honorManagement);
//     ExcelUtil<HonorManagement> util = new ExcelUtil<HonorManagement>(HonorManagement.class);
//     util.exportExcel(response, list, "荣誉资质信息数据");
// }
     @Log(title = "荣誉资质信息", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
     @Anonymous
     public void export(HttpServletResponse response, EnterpriseHonorVO enterpriseHonorVO)
     {
         List<HonorManagement> list =honorManagementService.selectHonorEnterPriseList(enterpriseHonorVO);
         ExcelUtil<HonorManagement> util = new ExcelUtil<HonorManagement>(HonorManagement.class);
         util.exportExcel(response, list, "荣誉资质信息数据");
     }



    /**
     * 获取荣誉资质信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('Honor:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(honorManagementService.selectHonorManagementById(id));
    }


    /**
     * 新增荣誉资质信息
     */
   // @PreAuthorize("@ss.hasPermi('Honor:management:add')")
    @Log(title = "荣誉资质信息", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody HonorManagement honorManagement) throws NoSuchFieldException, IllegalAccessException {
        //插入新的数据
        honorManagement.setStatus("0");
        int i = honorManagementService.insertHonorManagement(honorManagement);
        return toAjax(i);
    }

    /**
     * 修改荣誉资质信息
     */
    //@PreAuthorize("@ss.hasPermi('Honor:management:edit')")
    @Log(title = "荣誉资质信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult edit(@RequestBody HonorManagement honorManagement)
    {
        int i = honorManagementService.updateHonorManagement(honorManagement);
        return toAjax(i);
    }

    /**
     * 删除荣誉资质信息
     */
    //@PreAuthorize("@ss.hasPermi('Honor:management:remove')")
    @Log(title = "荣誉资质信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable String[] ids)
    {
       //查询原有的数据
        return toAjax(honorManagementService.deleteHonorManagementByIds(ids));
    }

     /**
      * 查询荣誉资质类型，以树结构返回
      */
     @GetMapping("/treeHonor")
     public AjaxResult treeHonor(){
         return honorManagementService.selectHonorManagementByType();
     }

     /**
      * 接收别人录入的荣誉资质内容
      */
     @GetMapping("/receiveHonor/{id}")
     public AjaxResult receiveHonor(@PathVariable String id){
         return toAjax(honorManagementService.updateReceiveHonor(id));
     }
     /**
      * 获取荣誉资质证书
      */
     @GetMapping("/getTypeList")
     public List getTypeList(){
       return honorManagementService.selectHonorManagementByTypeList();
     }

     /**
      * 获取根据证书类别查询证书url
      */
     @GetMapping("/getTypeUrlList")
     public TableDataInfo getTypeUrlList(HonorManagement honorManagement){
         startPage();
         honorManagement.setOrganizationId(SecurityUtils.getOrgId());
         honorManagement.setEnterpriseName(SecurityUtils.getOrgName());
         TableDataInfo tableDataInfo = honorManagementService.selectHonorManagementByTypeUrlList(honorManagement);
        return tableDataInfo;
     }

     /**
      * 获取所有客户的荣誉资质数据包括客户自身录入的
      */
     @GetMapping("/getHonorEnterPriseList")
     public TableDataInfo getHonorEnterPriseList(EnterpriseHonorVO enterpriseHonorVO,@RequestParam Integer pageNum, @RequestParam Integer pageSize){
//         startPage();
       List<HonorManagement> list =honorManagementService.selectHonorEnterPriseList(enterpriseHonorVO);
         int start = (pageNum - 1) * pageSize;
         int end = Math.min(start + pageSize, list.size());
         List<HonorManagement> pageList = list.subList(start, end);
         TableDataInfo rspData = new TableDataInfo();
         rspData.setCode(HttpStatus.SUCCESS);
         rspData.setMsg("查询成功");
         rspData.setRows(pageList);
         rspData.setTotal(new PageInfo(list).getTotal());
         return rspData;
     }
     /**
      * 获取所有合作商的荣誉资质数据
      */
     @GetMapping("/getCooperationList")
     public TableDataInfo getCooperationList(EnterpriseHonorVO enterpriseHonorVO, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
         List<HonorManagement> list=honorManagementService.selectCooperationList(enterpriseHonorVO);
         int start = (pageNum - 1) * pageSize;
         int end = Math.min(start + pageSize, list.size());
         List<HonorManagement> pageList = list.subList(start, end);
         TableDataInfo rspData = new TableDataInfo();
         rspData.setCode(HttpStatus.SUCCESS);
         rspData.setMsg("查询成功");
         rspData.setRows(pageList);
         rspData.setTotal(new PageInfo(list).getTotal());
         return rspData;
     }

     /**
      * 获取平台管理的荣誉资质数据
      */
     @GetMapping("/GetPlatformList")
     public TableDataInfo getCooperationList(EnterpriseHonorVO enterpriseHonorVO){
         startPage();
         List<HonorManagement> list=honorManagementService.selectPinTaiList(enterpriseHonorVO);
         return getDataTable(list);
     }

     /**
      * 根据id查询是否为客户数据还是公海数据
      */
     @GetMapping("/getEnterpriseType/{id}")
     public AjaxResult getEnterpriseType(@PathVariable String id){
         return AjaxResult.success(honorManagementService.selectEnterpriseType(id));
     }

     /**
      * 更新荣誉资质公开状态
      */
     @Log(title = "荣誉资质信息", businessType = BusinessType.UPDATE)
     @PostMapping("/updateCertificatePublicStatus")
     @Transactional(rollbackFor = Exception.class)
     public AjaxResult updateStatus(@RequestBody HonorPublicVO honorManagement){
         int i = honorManagementService.updateHonorStatus(honorManagement);
         return toAjax(i);
     }

     /**
      * 查询当前机构所有公开的荣誉资质
      */
     @GetMapping("/getPublicList")
     public List<SysUploadFile> getPublicList(){
       return honorManagementService.selectHonorManagementListPublic();
         }

     /**
      * 查询项目的交付物是否同步到荣誉资质
      */
     @GetMapping("/checkSync/{projectId}")
     public AjaxResult check(String projectId) {
         return success(honorManagementService.checkSync(projectId));
     }






 }
